package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"yk1/appconfig"
)

var (
	DB  *gorm.DB
	err error
)

func Init() {
	Confmysql := appconfig.Config.Mysql
	// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/res?charset=utf8mb4&parseTime=True&loc=Local",
		Confmysql.User, Confmysql.Password, Confmysql.Host, Confmysql.Port)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("mysql数据库连接失败")
		return
	}
	fmt.Println("mysql数据库连接成功")
	err = DB.AutoMigrate(&Dog{}, &Mastars{}, &User{})
	if err != nil {
		fmt.Println("迁移失败")
	} else {
		fmt.Println("迁移成功")
	}
}
